package com.limedia.explorer.entity;

import android.graphics.Color;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.limedia.explorer.enums.Enums.ClientNo;
import com.limedia.explorer.enums.Enums.TSERTPlayState;
import com.limedia.explorer.interfaces.TSIPlayerStateListener;
import com.thunder.rebot.rebotclient.R;

import limedia.sdk.api.realtime.TSPlayerDTO;
import limedia.sdk.api.realtime.TSRealTimePlayer;

public class ClientView implements SurfaceHolder.Callback {

	private static final String TAG = ClientView.class.getSimpleName();
	private View mBackgroundView = null;
	private SurfaceView mSurfaceView = null;
	private Surface mSurface = null;
	private SurfaceHolder mSurfaceHolder = null;
	private TextView loadingTextView = null;
	private ImageView loadingView = null;
	private Animation loadingAnimation = null;
	
	private String clientID = null;
	private ClientNo mClientNo = null;
	
	private boolean isSurfaceCreated = false;
	private boolean isDeviceAttached = false;
	private boolean isUploading = false;
	
	private String rtspUrl = null;
	private TSPlayerDTO mPlayerDTO = null;
	
	private TSIPlayerStateListener mPlayerStateListener = null;
	private TSRealTimePlayer mRealTimePlayer = null;
	
	public ClientView() {
		super();
	}
	
	public void setLoadingParam(TextView textView, ImageView loadingView, Animation loadingAnimation) {
		this.loadingView = loadingView;
		this.loadingAnimation = loadingAnimation;
		this.loadingTextView = textView;
	}
	
//	public void setLoadingAnimation(Animation loadingAnimation) {
//		this.loadingAnimation = loadingAnimation;
//	}

	public void setBackgroundView(View view) {
		this.mBackgroundView = view;
	}

	public void setmSurfaceView(SurfaceView mSurfaceView) {
		this.mSurfaceView = mSurfaceView;
		this.mSurfaceHolder = this.mSurfaceView.getHolder();
		this.mSurfaceHolder.addCallback(this);
	}

	public void setRtspUrl(String rtspUrl) {
		this.rtspUrl = rtspUrl;
		this.mPlayerDTO = new TSPlayerDTO();
	}

	public void setClientID(String clientID) {
		this.clientID = clientID;
	}
	
	public void setmClientNo(ClientNo mClientNo) {
		this.mClientNo = mClientNo;
	}
	
	public boolean isSurfaceCreated() {
		return this.isSurfaceCreated;
	}
	
	public void setDeviceAttached(boolean isDeviceAttached) {
		this.isDeviceAttached = isDeviceAttached;
	}
	
	public void startLoading() {
		Log.d(TAG, "startLoading()");
		if(null != this.loadingView && null != this.loadingAnimation) {
			this.loadingView.clearAnimation();
			this.loadingView.startAnimation(this.loadingAnimation);
		} else {
			Log.d(TAG, "View or animation is null!");
		}
	}
	
	private void stopLoading() {
		Log.d(TAG, "stopLoading()");
		if(null != this.loadingView) {
			this.loadingView.clearAnimation();
			this.loadingView.setVisibility(View.GONE);
			this.loadingTextView.setVisibility(View.GONE);
		} else {
			Log.d(TAG, "loadingView is null!");
		}
	}

	public void startPlayer() {
		this.stopLoading();
		
		if (null == this.mPlayerStateListener) {
			this.mPlayerStateListener = new TSIPlayerStateListener() {
				@Override
				public void onStop() {
					Log.d("RealTimePlayer", "onStop");
				}

				@Override
				public void onPrepareComplete() {
					if (null != mRealTimePlayer && !mRealTimePlayer.isPlaying()) {
						mRealTimePlayer.start();
						Log.e("RTSPplayerStart", "RTSPplayerStart");
					}
					Log.d("RealTimePlayer", "onPrepareComplete");
				}

				@Override
				public void onPlay() {
					Log.d("RealTimePlayer", "onPlay");

				}

				@Override
				public void onError(TSERTPlayState state) {
					Log.d("RealTimePlayeronError", state + "");
				}
			};
		}
		if (null == this.mRealTimePlayer) {
			this.mRealTimePlayer = new TSRealTimePlayer(this.rtspUrl, this.mPlayerDTO);
		}
		this.mRealTimePlayer.setClientNo(this.mClientNo);
		this.mRealTimePlayer.setDecodeType(this.mPlayerDTO.getDecodeType());
		this.mRealTimePlayer.setSurface(this.mSurface);
		this.mRealTimePlayer.setPlayerListener(this.mPlayerStateListener);
		this.mRealTimePlayer.prepare();
		Log.e("playerStart", "mRealTimePlayer.prepare()");
		Log.e("playerStart", this.mPlayerDTO.getDecodeType().name());
	}
	
	public void releasePlayer() {
		Log.i("releasePlayer", "release " + this.clientID);
		this.mSurface = null;
		if (null != this.mRealTimePlayer) {
			if (this.mRealTimePlayer.isPlaying()) {
				this.mRealTimePlayer.stop();
			}
			this.mRealTimePlayer.release();
			this.mRealTimePlayer = null;
		}
	}
	
	public boolean isUploading() {
		return isUploading;
	}

	public void startUpload() {
		this.isUploading = true;
		if(null != this.mBackgroundView) {
			this.mBackgroundView.setBackgroundResource(R.drawable.live_bg);
		}
		
		// TODO
//		if(!this.isUploading && this.isPlaying()) {
			this.mRealTimePlayer.startUpload();
//			this.isUploading = true;
//		}
	}
	
	public void stopUpload() {
		if(this.isUploading) {
			this.isUploading = false;
			if(null != this.mBackgroundView) {
				this.mBackgroundView.setBackgroundColor(Color.TRANSPARENT);
			}
		}
		
		// TODO
//		if(this.isUploading && null != this.mRealTimePlayer) {
			this.mRealTimePlayer.stopUpload();
//			this.isUploading = false;
//		}
	}
	
	public boolean isPlaying() {
		if(null == this.mRealTimePlayer || !this.mRealTimePlayer.isPlaying()) {
			return false;
		}
		return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		this.isSurfaceCreated = true;
		this.mSurface = holder.getSurface();
		if(this.isDeviceAttached) {
			this.startPlayer();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		this.releasePlayer();
	}
}
